//
//  menuView.m
//  电商首页
//
//  Created by ibokan on 15/11/21.
//  Copyright © 2015年 Mac. All rights reserved.
//

#import "YJMenuView.h"
//下划线宽
#define LINEWIDTH 85
//下划线高
#define LINEHEIGHT 2

@interface YJMenuView()
@property(strong,nonatomic)UIScrollView *scrollView;
/** 存放所有按钮 */
@property(nonatomic,copy)NSMutableArray *buttons;
/** 下划线 */
@property(strong,nonatomic)UIImageView *line;
/** 存放当前选中按钮 */
@property(strong,nonatomic)UIButton *selectBtn;
@end

@implementation YJMenuView
+ (instancetype)menuWithFrame:(CGRect)frame titles:(NSArray *)titles
{
    return [[self alloc]initWithFrame:frame titles:titles];
}

-(instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titles
{
    if(self = [super initWithFrame:frame])
    {
        _buttons = [NSMutableArray array];
        self.backgroundColor = [UIColor whiteColor];

        _scrollView = [[UIScrollView alloc]initWithFrame:self.bounds];

        //设置菜单栏选项是否大于4，若大于4就设置滚动
        unsigned long num = titles.count > 4?4:titles.count;
        _scrollView.contentSize = CGSizeMake((IPHONE_W/num)*titles.count, 0);
        _scrollView.showsHorizontalScrollIndicator = NO;
        [self addSubview:_scrollView];

        for (int i = 0; i < titles.count; i++)
        {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(i*(IPHONE_W/num), 0, IPHONE_W/num, frame.size.height - 2);
            //内容
            [btn setTitle:titles[i] forState:UIControlStateNormal];
            //字体颜色
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];
            btn.titleLabel.font = [UIFont systemFontOfSize:16.0f];
            //按钮标志
            btn.tag = 10 + i;
            [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
            [_scrollView addSubview:btn];
            [_buttons addObject:btn];
            
            if (0 == i)
            {
                btn.enabled = NO;
                _selectBtn = btn;
                _line = [[UIImageView alloc]init];
                _line.width = LINEWIDTH;
                _line.height = LINEHEIGHT;
                _line.centerX = btn.centerX;
                _line.y = CGRectGetMaxY(btn.frame);

                _line.backgroundColor = [UIColor whiteColor];
                [_scrollView addSubview:_line];
            }
        }
    }
    return self;
}

-(void)btnAction:(UIButton *)button
{
    //改变按钮的状态
    _selectBtn.enabled = YES;
    button.enabled = NO;
    _selectBtn = button;
    //更改下划线位置
    self.line.centerX = button.centerX;
    
    if([self.delegate respondsToSelector:@selector(menuView:clickButton:)])
    [self.delegate menuView:self clickButton:button];

}

-(void)setTitleNomalColor:(UIColor *)titleNormalColor
{
    for (UIButton *btn in self.buttons)
    [btn setTitleColor:titleNormalColor forState:UIControlStateNormal];

}

//选中情况下的颜色
-(void)setTitleSelectColor:(UIColor *)titleSelectColor
{
    for (UIButton *btn in self.buttons)
    [btn setTitleColor:titleSelectColor forState:UIControlStateDisabled];
}

//下划线的颜色
-(void)setLineColor:(UIColor *)lineColor
 {
     self.line.backgroundColor = lineColor;
    
 }


@end
